﻿using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.EventTemplate.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowEventTemplateDAL : BasicDAL<DBFlowEventTemplate, long>, IFlowEventTemplateDAL
    {
        public FlowEventTemplateDAL ( IRepository<DBFlowEventTemplate> basicDAL ) : base(basicDAL, "inter")
        {
        }
        public long Add ( EventTemplateAdd data )
        {
            DBFlowEventTemplate db = data.ConvertMap<EventTemplateAdd, DBFlowEventTemplate>();
            db.Id = IdentityHelper.CreateId();
            db.CreateTime = DateTime.Now;
            db.Status = FlowInterStatus.起草;
            this._BasicDAL.Insert(db);
            return db.Id;
        }
        public Result[] Query<Result> ( InterQueryParam query, IBasicPage paging, out int count ) where Result : class
        {
            paging.InitOrderBy("Id", true);
            return this._BasicDAL.Query<Result>(query.ToWhere(), paging, out count);
        }

        public void SetStatus ( DBFlowEventTemplate source, FlowInterStatus status )
        {
            if ( !this._BasicDAL.Update(a => a.Status == status, a => a.Id == source.Id) )
            {
                throw new ErrorException("flow.inter.status.set.fail");
            }
        }
    }
}
